package com.cms.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.functors.SwitchTransformer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cms.common.constant.Constant;
import com.cms.common.kit.ResponseKit;
import com.cms.common.kit.StrKit;
import com.cms.common.kit.Tree;
import com.cms.common.kit.TreeBuilder;
import com.cms.common.util.pass.JsonUtil;
import com.cms.controller.BaseController;
import com.cms.model.Category;
import com.cms.model.Content;
import com.cms.plugin.db.Page;
import com.cms.validate.Rule;
import com.cms.validate.ValidForm;


@Controller
@RequestMapping(value = "/admin/content")
public class ContentController extends BaseController{

	private static final String LIST_PAGE="admin/content/content_list";
	private static final String ADD_PAGE="admin/content/content_add";
	private static final String ADD_PAGE_PICTRUE="admin/content/pictrue_add";
	
	/**
	 * MENU_content
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/categorys")
	public String categorys(Map<String, Object> map,HttpServletRequest request) {
		Page<Category> paginate = Category.dao.paginate(1, 100);
		List<Tree> buildListToTree = TreeBuilder.buildCateToTree(paginate.getList());
		map.put("cate_tree",JsonUtil.toJson(buildListToTree));
		return "admin/content/menu_tree";
	}
	
	
	@RequestMapping(value = "/index")
	public String index(Map<String, Object> map,HttpServletRequest request) {
		Page<Category> paginate = Category.dao.paginate(1, 100);
		List<Tree> buildListToTree = TreeBuilder.buildCateToTree(paginate.getList());
		map.put("cate_tree",JsonUtil.toJson(buildListToTree));
		return LIST_PAGE;
	}
	
	@RequestMapping(value = "/frame")
	public String frame(Map<String, Object> map,String page,String category,String title,HttpServletRequest request) {
		map.put("category", category);
		map.put("title", title);
		Category currentCate = Category.dao.findById(category);
		if(currentCate!=null){
			map.put("type",currentCate.getInt("type"));
		}
		map.put("page", Content.dao.paginate(toInt(page, 1), category,title));
		return "admin/content/content_inner_frame";
	}
	@RequestMapping(value = "/quickView")
	public @ResponseBody String quickView(String id){
		Content w = Content.dao.findById(id);
		return ResponseKit.responseSuccess(JsonUtil.toJson(w.getAttrs()));
	}
	
	
	@RequestMapping(value = "/toAdd")
	public String toAdd(Map<String, Object> map,String id,String category,HttpServletRequest request) {
		String to_page=ADD_PAGE;
		Content w = Content.dao.findById(id);
		map.put("bean", w);
		//获取分类
		Page<Category> paginate = Category.dao.paginate(1, 100);
		List<Tree> buildListToTree = TreeBuilder.buildCateToTree(paginate.getList());
		map.put("cate_tree",JsonUtil.toJson(buildListToTree));
		if(w!=null){
			category=w.getInt("category_id")+"";
		}
		Category currentCate = Category.dao.findById(category);
		int type=currentCate.getInt("type");
		switch (type) {
			case Constant.CATEGORY_TYPE_PICTRUE:
				to_page=ADD_PAGE_PICTRUE;
				break;
			case Constant.CATEGORY_TYPE_ARTICLE:
				to_page=ADD_PAGE;
				break;
		}
		map.put("type",type);
		map.put("category",category);
		return to_page;
	}
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(Map<String, Object> map,String ids,HttpServletRequest request) {
		return Content.dao.deleteByIds(ids)==true?ResponseKit.responseSuccess("操作成功!"):ResponseKit.responseError("操作失败！");
	}
	
	
	@RequestMapping(value = "/add")
	public @ResponseBody String add(Map<String, Object> map,String id,HttpServletRequest request) {
		List<Rule> rules=new ArrayList<>();
		rules.add(new Rule().setField("title").setLabel("标题").setRules("required"));
		String errorMsg = ValidForm.validform(request, rules);
		if(!StrKit.isBlank(errorMsg)){
			return ResponseKit.responseError(errorMsg);
		}
		boolean success=false;
		if(StrKit.isBlank(id)){
			Content model = getModel(Content.class);
			model.set("created", new Date());
			model.set("modified", new Date());
			success=model.save();
		}else{
			Content model = getModel(Content.class);
			model.set("modified", new Date());
			success=model.update();
		}
		return success==true?ResponseKit.responseSuccess("操作成功!"):ResponseKit.responseError("操作失败！");
	}
	
}
